package mobile.persistencia;

import waba.io.*;
import waba.util.Vector;
import waba.sys.*;
import mobile.dominio.*;
import moio.util.*;

public class PSaldoEnvase extends Catalog {
   private ResizeRecord rs;
   private DataStream ds;
   
   public static PSaldoEnvase instance = new PSaldoEnvase();
   
   /**
    * Constructor which creates or open ( if it already exists ) the Customers.pdb file,
    * attach the ResizeRecord and DataStream. The pragmatic way of reading/writing to a
    * Catalog is by attaching those streams (ResizeRecord/DataStream) so that the resizing
    * of the Catalog and information writing/reading are performed by those classes
    */
   public PSaldoEnvase(){
      super("SaldoEnvase.mGqv.DATA", Catalog.CREATE);
	  //super("SaldoEnvase."+ Settings.appCreatorId +".DATA", Catalog.CREATE);
      rs = new ResizeRecord(this, 128);
      ds = new DataStream(rs);
   }
   
   public void write(SaldoEnvase unSaldoEnvase, boolean update){
      try {
	      if (update){
	         int n = indexOf(unSaldoEnvase);
	         if (n == -1 || !rs.restartRecord(n)) {
	        	 return;
	         }
	      }else{
	    	  rs.startRecord(); // start the record at the next available position

		      unSaldoEnvase.saveTo(ds);
		      rs.endRecord(); // finishes the writing of a record
	      }
      } catch (IOError e) {
    	  
      }
   }

   /**
    * Removes a record ( Customer ) from the pdb file
    * 
    * @param cli
    *           the Customer
    */
   public void remove(SaldoEnvase unSaldoEnvase){
      /*
       * We must set the record position to the record we want to delete, and then delete
       * the record.
       */
      int n = indexOf(unSaldoEnvase);
      try {
    	  if (n >= 0 && this.setRecordPos(n)) {
    		  this.deleteRecord();
    	  }
      }catch (IOError e) {
    	  
      }
   }
   
   /**
    * Find the index ( position ) of the record ( Customer ) inside the pdb file
    * 
    * @param cli
    *           the Customer
    * @return the index position or -1 if the Customer was not found on the record
    */
   public int indexOf(SaldoEnvase unSaldoEnvase){
      for (int n = this.getRecordCount() - 1; n >= 0; n--)
      try {
         if (this.setRecordPos(n)) {
        	 SaldoEnvase elSaldoEnvase = read();

            if (elSaldoEnvase.getProducto().getId() == unSaldoEnvase.getProducto().getId() & elSaldoEnvase.getIdCliente() == unSaldoEnvase.getIdCliente()){
            	return n;
            }
         }
      } catch (IOError e) {
    	  
      }
      return -1;
   }
   
   /**
    * Reads a record ( Customer ) and construct a Customer. Notice that the order in which
    * you read informations from the Customer is relevant.
    * 
    * @return the Customer
    */
   public SaldoEnvase read(){
      return new SaldoEnvase(ds);
   }
   
   public ArrayList saldoEnvaseXCliente(Cliente unCliente){
	   ArrayList colSaldoEnvase = new ArrayList();
	   
	   int n = this.getRecordCount();
	   
	   for (int i = 0; i < n; i++){
		   try {
			   if (this.setRecordPos(i)){
				   SaldoEnvase unSaldoEnvase = read();
				   if(unSaldoEnvase.getIdCliente() == unCliente.getId()){
					   colSaldoEnvase.add(unSaldoEnvase);
				   }
			   }
		   } catch (IOError e) {
			   
		   }
	   }
	   return colSaldoEnvase;
   }
}
